package com.loongnoy.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.converter.SimpleMessageConverter;

@Configuration
public class RabbitMQConfig {

    // 定义Direct类型交换机
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("direct_exchange", true, false); // 持久化，非自动删除
    }

    // 定义短信队列（sms_queue）
    @Bean
    public Queue smsQueue() {
        return new Queue("sms_queue", true); // 持久化队列
    }

    // 定义邮件队列（email_queue）
    @Bean
    public Queue emailQueue() {
        return new Queue("email_queue", true); // 持久化队列
    }

    // 绑定短信队列到交换机，路由键为"sms"
    @Bean
    public Binding smsBinding() {
        return BindingBuilder.bind(smsQueue()).to(directExchange()).with("sms");
    }

    // 绑定邮件队列到交换机，路由键为"email"
    @Bean
    public Binding emailBinding() {
        return BindingBuilder.bind(emailQueue()).to(directExchange()).with("email");
    }

    // 配置JSON消息转换器（使用Jackson）

    @Bean
    public org.springframework.amqp.support.converter.MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
